package com.gxar.quick.ar.server.db.service;

import com.github.pagehelper.PageHelper;
import com.gxar.common.entity.SlippageResult;
import com.gxar.common.entity.login.UserProfile;
import com.gxar.common.utils.SnowFlakeUtils;
import com.gxar.fusion.redis.service.RedisService;
import com.gxar.quick.ar.server.base.common.constant.RedisConstant;
import com.gxar.quick.ar.server.base.common.constant.WorksConstant;
import com.gxar.quick.ar.server.base.common.enums.ComboGradeEnum;
import com.gxar.quick.ar.server.base.model.combo.mapper.ComboMapStruct;
import com.gxar.quick.ar.server.base.model.combo.vo.response.ComboListResponse;
import com.gxar.quick.ar.server.db.mapper.ComboMapper;
import com.gxar.quick.ar.server.db.model.ComboEntity;
import com.gxar.quick.ar.server.db.repository.ComboRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.common.utils.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Optional;

/**
 * @author linsy
 * @version 1.0
 * @created 2023/11/6 17:58
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class ComboService {

    private final RedisService redisService;
    private final ComboMapper comboMapper;
    private final ComboRepository comboRepository;

    /**
     * 套餐列表查询
     *
     * @param userProfile 用户信息
     * @param nextId      下一页id
     * @return 响应
     */
    public SlippageResult<ComboListResponse> list(UserProfile userProfile, String nextId) {
        int pageNumber = 1;
        if (StringUtils.isNotBlank(nextId)) {
            String pageKey = String.format(RedisConstant.COMBO_PAGE_KEY, nextId);
            Object pageInfo = redisService.get(pageKey);
            if (pageInfo != null) {
                pageNumber = Integer.parseInt(pageInfo.toString());
                redisService.remove(pageKey);
            }
        }
        PageHelper.startPage(pageNumber, WorksConstant.DEFAULT_QUERY_LIMIT);
        List<ComboEntity> comboEntities = comboMapper.queryPage();
        if (CollectionUtils.isEmpty(comboEntities)) {
            return SlippageResult.build(null, Collections.emptyList());
        }
        List<ComboListResponse> response = ComboMapStruct.INSTANCE.entityToListResponses(comboEntities);
        String nextedId = SnowFlakeUtils.getInstance().nextId();
        String pageKey = String.format(RedisConstant.COMBO_PAGE_KEY, nextedId);
        redisService.set(pageKey, pageNumber + 1);
        redisService.expire(pageKey, RedisConstant.DEFAULT_EXPIRE_SECONDS);
        return SlippageResult.build(nextedId, response);
    }

    public ComboEntity getById(Long id) {
        Optional<ComboEntity> comboEntityOptional = comboRepository.findById(id);
        return comboEntityOptional.orElse(null);
    }

    /**
     * 根据code查询套餐
     *
     * @param code code
     * @return 响应
     */
    public ComboEntity findByGrade(Integer code) {
        return comboMapper.findByGrade(code);
    }
}
